package org.dromara.run.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.dromara.run.domain.bo.RuleBo;
import org.springframework.stereotype.Service;
import org.dromara.run.domain.bo.ActivityFormBo;
import org.dromara.run.domain.vo.ActivityFormVo;
import org.dromara.run.domain.ActivityForm;
import org.dromara.run.mapper.ActivityFormMapper;
import org.dromara.run.service.IActivityFormService;

import java.sql.Time;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 阳光跑跑步形式规则配置Service业务层处理
 *
 * @author chl
 * @date 2024-09-03
 */
@RequiredArgsConstructor
@Service
public class ActivityFormServiceImpl implements IActivityFormService {

    private final ActivityFormMapper baseMapper;

    /**
     * 查询阳光跑跑步形式规则配置
     */
    @Override
    public ActivityFormVo queryById(Long activityId){
        return baseMapper.selectVoById(activityId);
    }

    /**
     * 查询阳光跑跑步形式规则配置列表
     */
    @Override
    public TableDataInfo<ActivityFormVo> queryPageList(ActivityFormBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ActivityForm> lqw = buildQueryWrapper(bo);
        Page<ActivityFormVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询阳光跑跑步形式规则配置列表
     */
    @Override
    public List<ActivityFormVo> queryList(ActivityFormBo bo) {
        LambdaQueryWrapper<ActivityForm> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ActivityForm> buildQueryWrapper(ActivityFormBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ActivityForm> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getTenantId()), ActivityForm::getTenantId, bo.getTenantId());
        lqw.eq(bo.getActivityId()!=null, ActivityForm::getActivityId, bo.getActivityId());
        lqw.like(StringUtils.isNotBlank(bo.getActivityName()), ActivityForm::getActivityName, bo.getActivityName());
        lqw.eq(bo.getRequiredTimes() != null, ActivityForm::getRequiredTimes, bo.getRequiredTimes());
        lqw.eq(bo.getDistanceBegin() != null, ActivityForm::getDistanceBegin, bo.getDistanceBegin());
        lqw.eq(bo.getDistanceEnd() != null, ActivityForm::getDistanceEnd, bo.getDistanceEnd());
        lqw.eq(bo.getRunTimeBegin() != null, ActivityForm::getRunTimeBegin, bo.getRunTimeBegin());
        lqw.eq(bo.getRunTimeEnd() != null, ActivityForm::getRunTimeEnd, bo.getRunTimeEnd());
        lqw.eq(StringUtils.isNotBlank(bo.getPaceStandardBegin()), ActivityForm::getPaceStandardBegin, bo.getPaceStandardBegin());
        lqw.eq(StringUtils.isNotBlank(bo.getPaceStandardEnd()), ActivityForm::getPaceStandardEnd, bo.getPaceStandardEnd());
        return lqw;
    }

    /**
     * 新增阳光跑跑步形式规则配置
     */
    @Override
    public Boolean insertByBo(ActivityFormBo bo){
        this.tranData(bo);
        ActivityForm add = MapstructUtils.convert(bo, ActivityForm.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setActivityId(add.getActivityId());
        }
        return flag;
    }

    /**
     * 修改阳光跑跑步形式规则配置
     */
    @Override
    public Boolean updateByBo(ActivityFormBo bo){
        this.tranData(bo);
        ActivityForm update = MapstructUtils.convert(bo, ActivityForm.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }
    private void tranData(ActivityFormBo bo)  {
        Map<String, Object> params = bo.getParams();
        if(params.get("beginRunTime")!=null){
            String s = params.get("beginRunTime").toString();
            bo.setRunTimeBegin(Time.valueOf(s));
        }
        if(params.get("endRunTime")!=null){
            String s = params.get("endRunTime").toString();
            bo.setRunTimeEnd(Time.valueOf(s));
        }
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ActivityForm entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除阳光跑跑步形式规则配置
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
